<template>
  <div class="brand">
    <el-card shadow="never">
      <div slot="header">
        <el-button
          type="primary"
          size="default"
          @click="handleAdd"
        >新增</el-button>
      </div>
      <el-table :data="list" border stripe>
        <el-table-column label="#" type="index" width="50" align="center" />
        <el-table-column
          label="品牌名称"
          prop="name"
          width="150"
          align="center"
        />
        <el-table-column
          label="展示状态"
          prop="showStatus"
          width="200"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              是否显示:
              <el-switch
                v-model="scope.row.showStatus"
                :active-value="1"
                :inactive-value="0"
                @change="
                  handleShowStatusChange(scope.row.id, scope.row.showStatus)
                "
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="logo" width="200" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.logo" alt="" srcset="" class="table-icon">
          </template>
        </el-table-column>
        <el-table-column label="专区大图" width="200" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.bigPic" alt="" srcset="" class="table-icon">
          </template>
        </el-table-column>
        <el-table-column
          label="品牌故事"
          prop="brandStory"
          width="200"
          align="center"
        />
        <el-table-column
          label="创建时间"
          prop="createTime"
          width="200"
          align="center"
        />
        <el-table-column label="排序" prop="sort" width="150" />
        <el-table-column
          label="产品评论数量"
          prop="productCommentCount"
          width="150"
          align="center"
        />
        <el-table-column
          label="产品数量"
          prop="productCount"
          width="150"
          align="center"
        />
        <el-table-column label="是否品牌制造商" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.factoryStatus ? "是" : "不是" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="mini"
              style="color: red"
              @click="handleEdit"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <el-pagination
          :current-page.sync="page.start"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalNum"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-card>
    <BrandDetail ref="brandDetailRef" />
  </div>
</template>
<script>
import { findBrandByPage, switchShowStatus } from '@/api/product/brand/index'
import BrandDetail from './components/BrandDetail'
export default {
  components: {
    BrandDetail
  },
  data() {
    return {
      list: [],
      page: {
        start: 1,
        limit: 10,
        totalNum: 0
      }
    }
  },
  created() {
    this.getBrandList()
  },
  methods: {
    async getBrandList() {
      const { success, data, message } = await findBrandByPage(
        this.page.start,
        this.page.limit
      )
      if (success) {
        this.list = data.rows
        this.page.totalNum = data.total
      } else {
        this.$message.error(message)
      }
    },
    // 是否显示状态更改
    async handleShowStatusChange(id, status) {
      console.log(status)
      const { success, message } = await switchShowStatus({
        id,
        status
      })
      if (success) {
        this.$message.success('更新成功')
        this.getBrandList()
      } else {
        this.$message.error(message)
      }
    },
    // 新增
    handleAdd() {
      this.$refs.brandDetailRef.open()
    },
    handleEdit(item) {
      this.$refs.brandDetailRef.open(item)
    },
    sizeChange(newVal) {
      this.page.start = 1
      this.page.limit = newVal
      this.getBrandList()
    },
    currentChange(newVal) {
      this.page.start = newVal
      this.getBrandList()
    }
  }
}
</script>
<style lang="scss" scoped>
.brand {
  .table-icon {
    width: 100px;
    height: 100px;
  }
  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
